﻿//***************************************************************************//
//                                                                           //
//Author:       Kris Henry                                                   //
//Class Name:   OptionsState.cs                                              //
//Pupose:       Handle the drawing and input for the setting of the          //
//              ingame options                                               //
//Date :        December 2012                                                //
//                                                                           //
//***************************************************************************//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Indie_Game_Contest
{
    class OptionsState : IGameState
    {
        float volume = 100;
        KeyboardState keys;
        private KeyboardState prevkeys;
        GamePadState stick, prevstick;
        int option;

        public int Option
        {
            get { return option; }
            set { option = value; }
        }
        //GraphicsDeviceManager fullscreen = new GraphicsDeviceManager(Game1.Instance);
        bool screen;
        int optionFont;

        public int OptionFont
        {
            get { return optionFont; }
            set { optionFont = value; }
        }
        
        private static OptionsState instance;
        public static OptionsState Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new OptionsState();
                }
                return instance;
            }
        }
        public float Volume
        {
            get { return volume; }
            set { volume = value; }
        }
        override public void Update(float dt)
        {

        }
        public void SetFullScreen()
        {
            //Game1.Instance.Graphics.IsFullScreen = screen;
            Game1.Instance.Graphics.ToggleFullScreen();

        }
        override public bool Input()
        {
            AudioManager aManager = AudioManager.Instance;

            stick = GamePad.GetState(PlayerIndex.One);
            keys = Keyboard.GetState();
            if (CInput.Instance.KeyPressed(Keys.Down) || (CInput.Instance.KeyPressed(Keys.S)) || stick.DPad.Down == ButtonState.Pressed && prevstick.DPad.Down == ButtonState.Released)
            {
                aManager.PlaySFX("Menu_Move");
                option++;
                if (option > 2)
                    option = 0;
            }
            if (CInput.Instance.KeyPressed(Keys.Up) && keys.IsKeyDown(Keys.Up) || CInput.Instance.KeyPressed(Keys.W) && keys.IsKeyDown(Keys.W) ||
                stick.DPad.Up == ButtonState.Pressed && prevstick.DPad.Up == ButtonState.Released)
            {
                aManager.PlaySFX("Menu_Move");
                option--;
                if (option < 0)
                    option = 2;
            }

            if (option == 0)
            {
                if (CInput.Instance.KeyDown(Keys.Right) || CInput.Instance.KeyDown(Keys.D) || stick.DPad.Right == ButtonState.Pressed)
                {
                    AudioManager instance = AudioManager.Instance;
                    Volume++;
                    if (Volume > 100)
                        Volume = 100;
                    instance.AdjustSongVolume(Volume * 0.01f);
                    instance.AdjustSFXVolume(Volume * 0.01f);

                }
                if (CInput.Instance.KeyDown(Keys.Left) || CInput.Instance.KeyDown(Keys.A) ||  stick.DPad.Left == ButtonState.Pressed)
                {
                    AudioManager instance = AudioManager.Instance;
                    Volume--;
                    if (Volume < 0)
                        Volume = 0;
                    instance.AdjustSongVolume(Volume * 0.01f);
                    instance.AdjustSFXVolume(Volume * 0.01f);
                }
            }
            if(option == 1)
            {
                if (CInput.Instance.KeyPressed(Keys.Left) || CInput.Instance.KeyDown(Keys.A) ||  stick.DPad.Left == ButtonState.Pressed && prevstick.DPad.Left == ButtonState.Released)
                {
                    screen = false;
                    aManager.PlaySFX("Menu_Move");
                }
                if (CInput.Instance.KeyPressed(Keys.Right) || CInput.Instance.KeyDown(Keys.D) || stick.DPad.Right == ButtonState.Pressed && prevstick.DPad.Right == ButtonState.Released)
                {
                    screen = true;
                    aManager.PlaySFX("Menu_Move");
                   
                }
                if (CInput.Instance.KeyPressed(Keys.Enter) || stick.Buttons.A == ButtonState.Pressed && prevstick.Buttons.A == ButtonState.Released)
                {
                    aManager.PlaySFX("Menu_Enter");
                    SetFullScreen();
                }
                
            }
            if (option == 2)
            {
                if (CInput.Instance.KeyPressed(Keys.Enter) || stick.Buttons.A == ButtonState.Pressed && prevstick.Buttons.A == ButtonState.Released)
                {
                    Game1.Instance.AddState(MainMenuSate.Instance);
                    Game1.Instance.RemoveState(this);
                }
            }
                prevkeys = keys;
                prevstick = stick;
            return true;
        }
        override public void Render()
        {

            WorldData.spriteBatch.Draw(TextureManager.Instance.GetTexture(CTileManager.Instance.TileIDs["curtains"]),
             new Rectangle(-15, 0, WorldData.WindowWidth + 30, WorldData.WindowHeight + 25), Color.White);

            int nY = 200;

            if(option == 0)
                WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Master Volume:  " + Volume, new Vector2(50, nY), Color.Yellow);
            else
                WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Master Volume:  " + Volume, new Vector2(50, nY), Color.White);

            if (option == 1)
            {
                if (!screen)
                {
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Mode: ", new Vector2(50, nY+50), Color.Yellow);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Windowed", new Vector2(200, nY + 50), Color.Yellow);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "FullScreen ", new Vector2(400, nY + 50), Color.White);
                }
                else
                {
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Mode: ", new Vector2(50, nY + 50), Color.Yellow);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Windowed", new Vector2(200, nY + 50), Color.White);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "FullScreen ", new Vector2(400, nY + 50), Color.Yellow);
                }
            }
            else
            {
                if (!screen)
                {
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Mode: ", new Vector2(50, nY + 50), Color.White);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Windowed", new Vector2(200, nY + 50), Color.Yellow);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "FullScreen ", new Vector2(400, nY + 50), Color.White);
                }
                else
                {
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Mode: ", new Vector2(50, nY + 50), Color.White);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "Windowed", new Vector2(200, nY + 50), Color.White);
                    WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), "FullScreen ", new Vector2(400, nY + 50), Color.Yellow);
                }

            }
           if(option == 2)
               WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), " Back ", new Vector2(50, nY + 100), Color.Yellow);
           else
               WorldData.spriteBatch.DrawString(TextureManager.Instance.GetSpriteFont(OptionFont), " Back ", new Vector2(50, nY + 100), Color.White);

           

    
        }
        override public void Enter()
        {
            screen = false;
            WorldData.spriteBatch.GraphicsDevice.Reset();
            CInput.Instance.Clear();
        }
        override public void Exit()
        {
            AudioManager instance = AudioManager.Instance;

            Option = 0;
            instance.PlaySFX("Menu_Enter");
        }
        
    }
}
